function addSelectOption() {
    addPathSelect("#pathChoose","pathSelect");
}

function initial() {
    $("#backToView").hide();
}

function viewAllFiles() {
    $("#viewAll").click(function() {
    	$("#viewAllFiles").html("");
	$.ajax({
	    url : "../rest/csv/viewAllFiles",
	    data : {
		"path" : $("#pathSelect").val()
	    },
	    dataType : "json",
	    method : "post",
	    success : function(data) {	
			createTableForAllCsv();
			$.each(data, function(k, v) {
				debugger
			    $("#viewFile").append("<tr><td>" + v + "</td><td><input type=\"button\"  class=\"view-csv-file btn btn-default\" value=\"view\"  data-csvName=\"" + v + "\"" + "></td>" + "<td><input type=\"button\" class=\"delete-csv-file btn btn-default\" value=\"delete\" data-dleteCsvName=\"" + v + "\"" + "></td>" + "</tr>");

		});
		viewcsv();
		deleteOneFile();
		deleteAllFiles();
	    }
	});

    })
}

function viewcsv() {
    $(".view-csv-file").click(function() {
	createTableForOneCsv();
	$("#backToView").show();
	$("#deleteAllFiles").hide();
	$("#viewFile").hide();
	var fileName = $(this).data("csvname");
	var path = $("#pathSelect").val();
	var datasend = {
	    "fileName" : fileName,
	    "path" : path
	};
	$.ajax({
	    url : "../rest/csv/viewOneFile",
	    data : datasend,
	    dataType : "json",
	    method : "post",
	}).success(function(data) {

	    $.each(data, function(k, v) {

		if (k < 1) { 
			var tr="<tr>";
			$.each(v, function(a, b) {
				tr += "<th>" + b + "</th>";
			});
			tr += "</tr>";
		    $("#viewCsv-head").append(tr);
		} else {
			var tr="<tr>";
			$.each(v, function(a, b) {
				tr += "<td>" + b + "</td>";
			});
			tr += "</tr>";
		    $("#viewCsv-body").append(tr);
		}
	    });
	});

    });

}

function deleteOneFile() {
    $(".delete-csv-file").click(function() {

	var fileName = $(this).data("dletecsvname");
	var path = $("#pathSelect").val();
	var datasend = {
	    "fileName" : fileName,
	    "path" : path
	};
	$.ajax({
	    url : "../rest/csv/deleteOneFile",
	    data : datasend,
	    dataType : "json",
	    method : "post",
	})
	// window.location.href="../csv/uploadFileView";
	 $("#viewAll").click();
    });
}

function createTableForOneCsv() {
    var tableStructure = "<table  class='table table-bordered' id='viewCsv'>	<thead id='viewCsv-head'></thead><tbody id='viewCsv-body'></tbody></table>";

    $("#viewOneCsvFile").append(tableStructure);

}

function createTableForAllCsv() {
    var tableStructure = "<table  class='table table-bordered' id='viewFile'><tr><th>File</th><th>View</th><th>Delete</th></tr></table>";

    $("#viewAllFiles").append(tableStructure);

}

function returnback() {
    $("#returnback").click(function() {
	$("#viewOneCsvFile").html("");

	$("#backToView").hide();

	$("#viewFile").show();

	$("#deleteAllFiles").show();
    });
}

function deleteAllFiles() {
    var path = $("#pathSelect").val();
    var datasend = {
	"path" : path
    };
    $("#deleteAll").click(function() {
	$.ajax({
	    url : "../rest/csv/deleteAllFiles",
	    data : datasend,
	    method : "post",
	})
	// window.location.href="../csv/uploadFileView";
	$("#viewAll").click();
    });
}

$(function() {
    addSelectOption();
    initial();
    viewAllFiles();
    returnback();

});